/**
 * RegexPathSegment.java
 * 2016年6月4日
 * ©2015 北京特微智能科技有限公司. All rights reserved.
 */
package cn.trawe.pathmappingtree;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式Path片段
 * @author wangda
 */
public class RegexPathSegment extends PathSegment {
    
    String regex = "";
    Pattern pattern = null;
    
    public RegexPathSegment(String regex) {
        this.regex = regex == null? "": regex.trim();
        if (this.regex != null && this.regex.length() > 0) {
            pattern = Pattern.compile(this.regex);
        }
    }
    
    @Override
    public PathSegmentType getType() {
        return PathSegmentType.REGEX;
    }

    @Override
    public boolean isMatched(String matchStr) {
        if (pattern == null) {
            return false;
        }
        Matcher m = pattern.matcher(matchStr);
        return m.matches();
    }

    @Override
    public boolean isSameSegment(String segStr) {
        segStr = segStr == null? "": segStr.trim();
        return regex.equals(segStr);
    }

    @Override
    public String getValue() {
        return this.regex;
    }

}
